package com.campusevaluation.common.exception;

import cn.hutool.core.lang.hash.Hash;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class Result<T> {
    /**
     * 响应状态码
     * | 200  | OK                    | 请求成功
     * | ---- | --------------------- | ---------------------------------------------------
     * | 201  | CREATED               | 创建成功
     * | 204  | DELETED               | 删除成功
     * | 400  | BAD REQUEST           | 请求的地址不存在或者包含不支持的参数
     * | 401  | UNAUTHORIZED          | 未授权
     * | 403  | FORBIDDEN             | 被禁止访问
     * | 404  | NOT FOUND             | 请求的资源不存在
     * | 422  | Unprocesable entity   | [POST/PUT/PATCH] 当创建一个对象时，发生一个验证错误
     * | 500  | INTERNAL SERVER ERROR | 内部错误
     */
    private Integer code;
    private String msg;
    private T data;
    private Map<String, Object> map = new HashMap<>();

    public static <T> Result<T> success(String msg, T data) {
        Result<T> result = new Result<T>();
        result.data = data;
        result.msg = msg;
        result.code = 200;
        return result;
    }

    public static <T> Result<T> success(String msg, Map<String, Object> map) {
        Result<T> result = new Result<T>();
        result.map = map;
        result.msg = msg;
        result.code = 200;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<T>();
        result.msg = msg;
        return result;
    }

    public static <T> Result<T> error(String msg, Integer code) {
        Result<T> result = new Result<T>();
        result.code = code;
        result.msg = msg;
        return result;
    }

    public Result<T> add(String key, Object value) {
        map.put(key, value);
        return this;
    }
}
